const app = getApp();
const pageSize = 15;
const auditStatus = [{
  value: "",
  name: "全部"
}, {
  value: 1,
  name: "审核中"
}, {
  value: 2,
  name: "审核通过"
}, {
  value: 3,
  name: "审核拒绝"
}]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    examineList: [],
    page: 1, //当前页数
    drawer: false,
    isFilter: false,
    conditions: {
      start_time: '',
      end_time: '',
      card_type: '',
      village_name: '',
      audit_status_index: '',
      areaInfo_name: [],
      area_id_Info: [],
    },
    auditStatus,
    empty: false,
    loadmore: false,
    nomore: false,
    is_franchiser: "", //是否为加盟商角色 1是 ""不是
  },


  onReady: function () {
    this.cascade = this.selectComponent("#cascade");
  },

  showCascade() {
    this.cascade.selection(this.data.conditions.area_id_Info)
  },





  cascadeComplete(e) {
    const detail = e.detail;
    this.setData({
      'conditions.area_id_Info': detail.ids,
      'conditions.areaInfo_name': detail.names,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      is_franchiser: options.is_franchiser || ''
    })
    this.examineList();
  },

  reloadData() {
    this.setData({
      page: 1,
      empty: false,
      loadmore: false,
      nomore: false,
      examineList: []
    })
    wx.showLoading();
    this.examineList();
  },


  onPullDownRefresh() {
    this.reloadData();
  },

  onReachBottom: function () {
    if (this.data.loadmore) {
      this.setData({
        page: this.data.page + 1
      })
      this.examineList()
    }
  },



  // 打开详情
  openDetails: function (e) {
    wx.navigateTo({
      url: '../examineOnlineDetails/examineOnlineDetails?id=' + e.currentTarget.dataset.id + "&partner_id=" + e.currentTarget.dataset.partner_id
    })
  },
  //获取申请列表
  examineList: function () {

    let audit_status;
    if (this.data.conditions.audit_status_index !== '') {
      audit_status = this.data.auditStatus[this.data.conditions.audit_status_index].value;
    }
    const param = {
      row: pageSize,
      page: this.data.page,
      start_time: this.data.conditions.start_time,
      end_time: this.data.conditions.end_time,
      audit_status,
      province: this.data.conditions.area_id_Info[0] || '',
      city: this.data.conditions.area_id_Info[1] || '',
      area: this.data.conditions.area_id_Info[2] || '',
      card: this.data.conditions.card,
      card_class: 2,
      village_name: this.data.conditions.village_name
    }
    if (this.data.is_franchiser) {
      param.type = 1;
    }
    app.ajax({
      url: 'Partner/WaterCard/accountAuditList',
      method: "POST",
      data: param,
      success: res => {
        wx.hideLoading();
        wx.stopPullDownRefresh()
        if (res.data.code > 0) {
          let {
            data,
            page
          } = res.data.data;
          const list = this.data.examineList.concat(data);
          let total_number = Number.parseInt(page.total_number);
          this.setData({
            examineList: list,
            empty: total_number === 0 ? true : false,
            loadmore: list.length >= total_number ? false : true,
            nomore: list.length >= total_number ? true : false,
            drawer: false,
          });
        } else {
          app.showToast(res.data.message);
        }
      },
      error: e => {
        wx.hideLoading();
        wx.stopPullDownRefresh()
        app.showToast("网络错误");
      }
    })
  },



  handleTime(e) {
    const name = e.currentTarget.dataset.name;
    this.setData({
      [`conditions.${name}`]: e.detail.value
    })
  },



  handleChange(e) {
    const {
      index,
      name
    } = e.currentTarget.dataset;
    this.setData({
      [`conditions.${name}`]: index
    })
  },



  showDrawer() {
    this.condition = {
      ...this.data.conditions
    };
    this.setData({
      drawer: true,
    })
  },

  closeDrawer() {
    this.setData({
      drawer: false,
      conditions: this.condition
    })
    delete(this.condition)
  },

  resetDrawer() {
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] = "";
    }
    this.setData({
      conditions,
    })
    this.cascade.init();
  },

  confirmDrawer() {
    let isFilter = false;
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] !== "" && (isFilter = true);
    }
    this.setData({
      isFilter
    })
    delete(this.condition);
    this.reloadData();
  },

  //跳转到选择园区
  navMiddleVillage() {
    let _self = this;
    let url = "../middle/village/index?hidden=1"
    if (this.data.is_franchiser) {
      url += "&is_franchiser=1&mode=1"
    }
    wx.navigateTo({
      url,
      events: {
        acceptDataFromMiddleVillage: function (data) {
          if (data) {
            _self.village_id = data.id;
            _self.setData({
              [`conditions.village_name`]: data.village_name
            })
          }
        }
      },
      success: res => {
        res.eventChannel.emit("acceptDataFromOpenerPage", {
          village_id: this.village_id || ''
        })
      }
    })
  },


  operate(e) {
    // type 1审核 2修改 3查看
    let {
      id,
      type
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `../examineOnlineDetails/examineOnlineDetails?id=${id}&type=${type}${this.data.is_franchiser && "&is_franchiser=1"}`,
    })
  }

})